/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.brain.core.view.mbean;

import com.brain.core.dto.EntidadDto;

import com.brain.core.exception.DAOException;
import com.brain.core.manager.BitacoraManager;
import com.brain.core.manager.EntidadManager;
import com.brain.core.manager.RecursosManager;

import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import javax.faces.model.SelectItem;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 *
 * @author Indra
 */
@Controller("entidadMBean")
@Scope("session")
public class EntidadMBean extends GenericMBean implements Serializable {

    private static Logger logger = Logger.getLogger(EntidadMBean.class);
    @Resource
    private EntidadManager manager;
    @Resource
    private BitacoraManager bitacoraManager;
    @Resource
    private RecursosManager recursosManager;
    @Autowired
    private ApplicationMBean applicationMBean;
    @Autowired
    private SessionMBean sessionMBean;
    
    private List<EntidadDto> lista;
    private EntidadDto dto;
    private EntidadDto dtoFiltro;
    private String keyEntidad = "key.entidad.entidad";
    
    private SelectItem[] siTiposModulos;

    public EntidadMBean() {
        this.dtoFiltro = new EntidadDto();
    }

    public String iniciar() {
        this.lista = null;
        this.dto = null;
        this.dtoFiltro = new EntidadDto();

        return manager.getUrlLista();
    }
    
    public String nuevo() {
        sessionMBean.setAccion(applicationMBean.getNuevo());
        sessionMBean.setTitulo(recursosManager.construirTitulo(keyEntidad, sessionMBean.getAccion()));

        this.dto = manager.inicializar();
        return manager.getUrl();
    }

    public String editar() {
        sessionMBean.setAccion(applicationMBean.getEditar());
        sessionMBean.setTitulo(recursosManager.construirTitulo(keyEntidad, sessionMBean.getAccion()));        
        try {
            this.dto = manager.obtenerPorId(this.dto.getId());
        } catch (DAOException ex) {
            logger.error(ex);
        }
        return manager.getUrl();
    }

    public String ver() {
        sessionMBean.setAccion(applicationMBean.getVer());
        sessionMBean.setTitulo(recursosManager.construirTitulo(keyEntidad, sessionMBean.getAccion()));
        try {
            this.dto = manager.obtenerPorId(this.dto.getId());
        } catch (DAOException ex) {
            logger.error(ex);
        }
        return manager.getUrl();
    }

    public void buscar() {
        try {
            this.lista = this.manager.obtener(this.dtoFiltro);
        } catch (DAOException ex) {
            showError("ERROR_SISTEMA", ex.getMessage());
        }
    }

    public String guardar() {        
        manager.guardar(this.dto);                    
        return null;
    }
    
    public String verBitacora() {
        
        bitacoraManager.cargarBitacoraEntidad(manager.getIdEntidad(), this.dto.getId());
        return null;
    }


    public String retroceder() {
        buscar();
        return manager.getUrlLista();
    }

    
    /**GETTER AND SETTER**/  
    public static Logger getLogger() {
        return logger;
    }

    public static void setLogger(Logger logger) {
        EntidadMBean.logger = logger;
    }

    public EntidadManager getManager() {
        return manager;
    }

    public void setManager(EntidadManager manager) {
        this.manager = manager;
    }

    public BitacoraManager getBitacoraManager() {
        return bitacoraManager;
    }

    public void setBitacoraManager(BitacoraManager bitacoraManager) {
        this.bitacoraManager = bitacoraManager;
    }

    public RecursosManager getRecursosManager() {
        return recursosManager;
    }

    public void setRecursosManager(RecursosManager recursosManager) {
        this.recursosManager = recursosManager;
    }

    public ApplicationMBean getApplicationMBean() {
        return applicationMBean;
    }

    public void setApplicationMBean(ApplicationMBean applicationMBean) {
        this.applicationMBean = applicationMBean;
    }

    public SessionMBean getSessionMBean() {
        return sessionMBean;
    }

    public void setSessionMBean(SessionMBean sessionMBean) {
        this.sessionMBean = sessionMBean;
    }

    public List<EntidadDto> getLista() {
        return lista;
    }

    public void setLista(List<EntidadDto> lista) {
        this.lista = lista;
    }

    public EntidadDto getDto() {
        return dto;
    }

    public void setDto(EntidadDto dto) {
        this.dto = dto;
    }

    public EntidadDto getDtoFiltro() {
        return dtoFiltro;
    }

    public void setDtoFiltro(EntidadDto dtoFiltro) {
        this.dtoFiltro = dtoFiltro;
    }

    public String getKeyEntidad() {
        return keyEntidad;
    }

    public void setKeyEntidad(String keyEntidad) {
        this.keyEntidad = keyEntidad;
    }

    public SelectItem[] getSiTiposModulos() {
        return this.recursosManager.siTiposModulos();
    }

    public void setSiTiposModulos(SelectItem[] siTiposModulos) {
        this.siTiposModulos = siTiposModulos;
    }
    
}
